//
//  Util.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/1.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit
import CoreText

class Util: NSObject {
    fileprivate var more:MoreLabel = MoreLabel()
    
    static let shared:Util = {
        let util = Util()
        return util
    }()
    
    /// 设置uitableView自适应
    ///
    /// - Parameters:
    ///   - tableView: 需要设置的tableView对象
    ///   - estimatedRowHeight: tableViewRow的预计行高
    public func setTableViewAutolayout(tableView:UITableView , estimatedRowHeight:CGFloat){
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = estimatedRowHeight
    }
    
    public func hideKeyBoard(textField:UITextField){
        textField.resignFirstResponder()
    }
    
    public func hideKeyBoard(textView:UITextView){
        textView.resignFirstResponder()
    }
    
    public func updateLabelFrame(label:UILabel , maxWidth:CGFloat , line:Int){
        if label.text == "" && label.superview == nil{
            return
        }
        let string:NSString = label.text! as NSString
        let options:NSStringDrawingOptions = .usesLineFragmentOrigin
        let boundingRect = string.boundingRect(with: CGSize.init(width: maxWidth, height: 0), options: options, attributes: [NSFontAttributeName:label.font], context: nil)
        label.numberOfLines = line
        label.frame = CGRect.init(x: label.frame.minX, y: label.frame.minY, width: boundingRect.size.width, height: boundingRect.size.height)
    }
    
    public func setShowMoreBtnForLabel(label:UILabel , onLines:Int = 3 , title:String = "... [双击展开]" , maxWidth:CGFloat = AppWidth - 40){
        if label.text == "" && label.superview == nil{
            return
        }
        let titleWidth = StringSizeEngine.calculateStringWidth(title, height: 10000, font: label.font)
        self.updateLabelFrame(label: label, maxWidth: maxWidth, line: onLines)
        
//        label.frame = CGRect.init(x: 60, y:  55 , width: boundingRect.size.width, height: boundingRect.size.height)
        
        let arr:Array = self.getSeparatedLines(label: label)
//        Log.shared.printLog("行数:  \(arr.count)")
        if arr.count > onLines{
//            label.isUserInteractionEnabled = true
            let lineStr:String =  arr[onLines - 1] as! String
            
            var i = 3
            var subStr = NSString(string: lineStr).substring(from: lineStr.characters.count  - i)
//            Log.shared.printLog("subStr：   \(subStr)")
            var subStrWidth = StringSizeEngine.calculateStringWidth(subStr, height: 10000, font: label.font)
            while subStrWidth <  titleWidth{
                i += 1
                subStr = NSString(string: lineStr).substring(from: lineStr.characters.count - i)
                subStrWidth = StringSizeEngine.calculateStringWidth(subStr, height: 10000, font: label.font)
            }
            
            let subContentStr = NSString(string: lineStr).substring(to: lineStr.characters.count - i)
            var spliceStr = ""
            for i in 0..<onLines - 1{
                if let lineStr:String =  arr[i] as? String{
                    spliceStr = spliceStr + lineStr
                }
            }
            spliceStr = spliceStr + subContentStr + title
            let nameAttrStr = NSMutableAttributedString.init(string: spliceStr)
            nameAttrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(spliceStr.characters.count - title.characters.count + 3 , title.characters.count - 3))
            label.attributedText = nameAttrStr
            
        }else{
        }
        
    }
    //MARK:获取label内容有多少行
    public func getSeparatedLines(label:UILabel) -> Array<Any>{
        return more.getSeparatedLines(from: label)
    }
    
    /// 根据屏幕大小等获取图片大小
    ///
    /// - Parameters:
    ///   - itemSpacing: item 之间的间距
    ///   - maxWidth: 最大宽度
    ///   - rowCount: 一行放几个cell
    /// - Returns: 单个图片的宽度
    public func getImgWidthForCollectionView(itemSpacing:CGFloat = 5 , maxWidth:CGFloat , rowCount:CGFloat) -> CGFloat{
        let width:CGFloat = (maxWidth - (rowCount - 1)*itemSpacing)/rowCount
        return width
    }
    
    
}
